import { Calendar } from "./calendar/calendar.js";
const createEditing = () => ({
    date: new Date().getTime(),
    title: "幸福",
    description: "文本描述",
    please: "祝福语",
    image: "",
    tags: [],
    belongTo: "",
    type: "card",
});
import { _ } from "./cloudInit.js";
// 心语签相关的数据存储
export default {
    state: () => ({
        allXin: {
            requestResult: Calendar,
            packageIndex: 0,
            firstPageShow: {},
        },

        editingContent: createEditing(),
    }),

    mutations: {
        firstPageXinSetTo(state, Xin) {
            // 为了可以让某些情况下直接设置心语签
            state.allXin.firstPageShow = Xin;
            state.allXin.requestResult.looking = Xin;
        },
        firstPageXinSwitchTo(state, _id) {
            state.allXin.firstPageShow = state.allXin.requestResult.look(_id);
        },
        resetEditingXin(state) {
            state.editingContent = createEditing();
        },
        changeEditingXin(state, res) {
            Object.entries(res).forEach(([key, value]) => {
                state.editingContent[key] = value;
            });
            console.log(state.editingContent);
        },
    },
    actions: {
        async UploadXin({ state }) {
            if (!state.editingContent.image) throw "没有添加图片";
            // 本地直接上传
            let image = state.editingContent.image;
            if (!/^cloud:\/\//.test(image)) {
                image = await new Promise((resolve, reject) => {
                    wx.cloud.uploadFile({
                        cloudPath: `Cards/${new Date().getTime()}-${
                            (Math.random() * 1000) | 0
                        }.jpg`,
                        filePath: image,
                        success({ fileID }) {
                            resolve(fileID);
                        },
                        fail() {
                            reject("图片上传错误");
                        },
                    });
                });
            }
            console.log("上传", state.editingContent);
            debugger;
            return wx.cloud
                .callFunction({
                    name: "UploadXin",
                    data: {
                        Xin: {
                            ...state.editingContent,
                            image,
                        },
                    },
                })
                .then((res) => console.log("上传成功", res));
        },
    },
};
